package com.parktech.sun.web.controller;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.SearchService;
import com.parktech.sun.domain.service.news.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * User: Think
 * Date: 11-1-21
 * Time: 上午1:52
 */
@Controller
@RequestMapping("/search")
public class SearchViewController extends BaseViewController {

    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private SearchService searchService;

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Autowired
    public SearchViewController(SearchService searchService) {
        this.searchService = searchService;
    }

    @RequestMapping(value="/{pageNo}", method = {RequestMethod.POST, RequestMethod.PUT})
    public String view(@PathVariable("pageNo") int pageNo, @RequestParam("keywords") String keywords, Model model) {
        Page page = new Page(pageSize, pageNo);
        model.addAttribute("keywords", keywords);
        model.addAttribute(searchService.search(keywords, page));
        return "search";
    }
}
